package com.loongtech.bi.action.biplayer;

import com.loongtech.bi.action.BaseBiSearchAction;
import com.loongtech.bi.manager.player.OnlinePlayerManager;
import com.loongtech.bi.manager.player.OnlinePlayerManager.OnlieTendData;
import com.loongtech.bi.manager.player.OnlinePlayerManager.OnlineChannelData;
import com.loongtech.bi.manager.player.OnlinePlayerManager.OnlinePlatFormData;
import com.loongtech.bi.manager.player.OnlinePlayerManager.OnlineServerData;
import com.loongtech.bi.support.chart.ChartTool;
import com.loongtech.bi.support.chart.ChartType;
import com.loongtech.bi.support.chart.ChartUIData;
import com.loongtech.core.util.Time;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 玩家分析-在线分析Action
 * @author Marin
 * @date 2015年09月17日
 */
@Controller
@Scope("session")
@RequestMapping("/player/onlinePlayer")
public class OnlinePlayerBiAction extends BaseBiSearchAction {
	private static final long serialVersionUID = 1L;
	private List<OnlieTendData> dataList1;  // 前端要显示的数据1
	private ChartUIData chartData1;  // 前端要显示的柱状图1数据
	//平台对比
	private List<OnlinePlatFormData> dataList2;
	private ChartUIData chartData2;
	//渠道对比
	private List<OnlineChannelData> dataList3;
	private ChartUIData chartData3;
	//区服对比
	private List<OnlineServerData> dataList4;
	private ChartUIData chartData4;
	@Autowired
	private OnlinePlayerManager onlinePlayerManager;

    @RequestMapping(value = "initParam.do", method = {RequestMethod.GET, RequestMethod.POST})
    public void initParam(HttpServletRequest request, HttpServletResponse response, String callback) {
        Map<String, Object> result = new HashMap<>();
        try {
            result.put("res", "0");
            result.put("serverItmes", serverItmes);
            result.put("channelItmes", channelItmes);
            result.put("platformItmes", platformItmes);
            printToJson(response, result, callback);
        } catch (Exception e) {
            e.printStackTrace();
            result.put("res", "1");
            result.put("msg", e.getMessage());
            printToJson(response, result, callback);
            return;
        }
    }

    @RequestMapping(value = "search.do", method = {RequestMethod.GET, RequestMethod.POST})
    public void search(HttpServletRequest request, HttpServletResponse response, @RequestParam String dstart, @RequestParam String dend, @RequestParam String platform, @RequestParam String channel, @RequestParam String server, String callback) {
        Map<String, Object> result = new HashMap<>();
        try {
            dateStart = Time.formatDate(dstart, Time.TIMEPATTERN_SIMPLE);
            dateEnd = Time.formatDate(dend, Time.TIMEPATTERN_SIMPLE);
            queryPlatform = platform;
            queryChannel = channel;
            queryServer = server;

            dataList1 = onlinePlayerManager.getOnlieTendData(dateStart, dateEnd, platform, channel, server);
            if (dataList1 != null && dataList1.size() > 0) {
                chartData1 = new ChartUIData();
                chartData1.setJsonType(ChartType.SPLINE.type);
                chartData1.setJsonStr(new ChartTool(dataList1, OnlieTendData.class, "").loadData(chartData1.getJsonType()));
                //下拉框
                chartData1.setAllItmes(ChartTool.getAllItems(OnlieTendData.class));
            } else {
                chartData1 = null;
            }

            //平台对比
            dataList2 = onlinePlayerManager.getOnlinePlatFormData(dateStart, dateEnd);
            if (dataList2 != null && dataList2.size() > 0) {
                chartData2 = new ChartUIData();
                chartData2.setJsonType(ChartType.COLUMN.type);
                chartData2.setJsonStr(new ChartTool(dataList2, OnlinePlatFormData.class, "").loadData(chartData2.getJsonType()));
                chartData2.setAllItmes(ChartTool.getAllItems(OnlinePlatFormData.class));
            } else {
                chartData2 = null;
            }

            //渠道对比
            dataList3 = onlinePlayerManager.getOnlineChannelData(dateStart, dateEnd);
            if (dataList3 != null && dataList3.size() > 0) {
                chartData3 = new ChartUIData();
                chartData3.setJsonType(ChartType.COLUMN.type);
                chartData3.setJsonStr(new ChartTool(dataList3, OnlineChannelData.class, "").loadData(chartData3.getJsonType()));
                chartData3.setAllItmes(ChartTool.getAllItems(OnlineChannelData.class));
            } else {
                chartData3 = null;
            }

            //区服对比
            dataList4 = onlinePlayerManager.getOnlineServerData(dateStart, dateEnd);
            if (dataList4 != null && dataList4.size() > 0) {
                chartData4 = new ChartUIData();
                chartData4.setJsonType(ChartType.COLUMN.type);
                chartData4.setJsonStr(new ChartTool(dataList4, OnlineServerData.class, "").loadData(chartData4.getJsonType()));
                chartData4.setAllItmes(ChartTool.getAllItems(OnlineServerData.class));
            } else {
                chartData4 = null;
            }

            result.put("res", "0");
            result.put("dataList1", dataList1);
            result.put("chartData1", chartData1);
            //result.put("dataList2", dataList2);
            result.put("chartData2", chartData2);
            //result.put("dataList3", dataList3);
            result.put("chartData3", chartData3);
            //result.put("dataList4", dataList4);
            result.put("chartData4", chartData4);

            printToJson(response, result, callback);
        } catch (Exception e) {
            e.printStackTrace();
            result.put("res", "1");
            result.put("msg", e.getMessage());
            printToJson(response, result, callback);
        }
	}
	
	//趋势图表的选择
    @RequestMapping(value = "updateTrendType.do", method = {RequestMethod.GET, RequestMethod.POST})
	public void updateTrendType(HttpServletRequest request, HttpServletResponse response, @RequestParam String dstart, @RequestParam String dend, @RequestParam String platform, @RequestParam String channel, @RequestParam String server, @RequestParam String trendType, String callback) {
        Map<String, Object> result = new HashMap<>();
        try {
            dateStart = Time.formatDate(dstart, Time.TIMEPATTERN_SIMPLE);
            dateEnd = Time.formatDate(dend, Time.TIMEPATTERN_SIMPLE);
            queryPlatform = platform;
            queryChannel = channel;
            queryServer = server;

            dataList1 = onlinePlayerManager.getOnlieTendData(dateStart, dateEnd, platform, channel, server);
            if (dataList1 != null && dataList1.size() > 0) {
                chartData1 = new ChartUIData();
                chartData1.setJsonType(ChartType.SPLINE.type);
                chartData1.setJsonStr(new ChartTool(dataList1, OnlieTendData.class, trendType).loadData(chartData1.getJsonType()));
                //下拉框
                chartData1.setAllItmes(ChartTool.getAllItems(OnlieTendData.class));
            } else {
                chartData1 = null;
            }
            result.put("chartData1", chartData1);

            printToJson(response, result, callback);
        } catch (Exception e) {
            e.printStackTrace();
            result.put("res", "1");
            result.put("msg", e.getMessage());
            printToJson(response, result, callback);
        }
	}

    @RequestMapping(value = "updateCompareType.do", method = {RequestMethod.GET, RequestMethod.POST})
	public void updateCompareType(HttpServletRequest request, HttpServletResponse response, @RequestParam String dstart, @RequestParam String dend, @RequestParam String compareType, @RequestParam String trendType, String callback) {
        Map<String, Object> result = new HashMap<>();
        try {
            dateStart = Time.formatDate(dstart, Time.TIMEPATTERN_SIMPLE);
            dateEnd = Time.formatDate(dend, Time.TIMEPATTERN_SIMPLE);

            if ("platform".equals(compareType)) {
                dataList2 = onlinePlayerManager.getOnlinePlatFormData(dateStart, dateEnd);
                if (dataList2 != null && dataList2.size() > 0) {
                    chartData2 = new ChartUIData();
                    chartData2.setJsonType(ChartType.COLUMN.type);
                    chartData2.setJsonStr(new ChartTool(dataList2, OnlinePlatFormData.class, trendType).loadData(chartData2.getJsonType()));
                    chartData2.setAllItmes(ChartTool.getAllItems(OnlinePlatFormData.class));
                } else {
                    chartData2 = null;
                }
                result.put("chartData", chartData2);
            } else if ("channel".equals(compareType)) {
                dataList3 = onlinePlayerManager.getOnlineChannelData(dateStart, dateEnd);
                if (dataList3 != null && dataList3.size() > 0) {
                    chartData3 = new ChartUIData();
                    chartData3.setJsonType(ChartType.COLUMN.type);
                    chartData3.setJsonStr(new ChartTool(dataList3, OnlineChannelData.class, trendType).loadData(chartData3.getJsonType()));
                    chartData3.setAllItmes(ChartTool.getAllItems(OnlineChannelData.class));
                } else {
                    chartData3 = null;
                }
                result.put("chartData", chartData3);
            } else if ("server".equals(compareType)) {
                dataList4 = onlinePlayerManager.getOnlineServerData(dateStart, dateEnd);
                if (dataList4 != null && dataList4.size() > 0) {
                    chartData4 = new ChartUIData();
                    chartData4.setJsonType(ChartType.COLUMN.type);
                    chartData4.setJsonStr(new ChartTool(dataList4, OnlineServerData.class, trendType).loadData(chartData4.getJsonType()));
                    chartData4.setAllItmes(ChartTool.getAllItems(OnlineServerData.class));
                } else {
                    chartData4 = null;
                }
                result.put("chartData", chartData4);
            }

            printToJson(response, result, callback);
        } catch (Exception e) {
            e.printStackTrace();
            result.put("res", "1");
            result.put("msg", e.getMessage());
            printToJson(response, result, callback);
        }
    }

    @RequestMapping(value = "updateServer.do", method = {RequestMethod.GET, RequestMethod.POST})
    public void updateServer(HttpServletRequest request, HttpServletResponse response, String server, String callback) {
        try {
            queryServer = server;
            selectedServer();
            Map<String, Object> result = new HashMap<>();
            result.put("res", "0");
            result.put("channelItmes", channelItmes);
            result.put("platformItmes", platformItmes);
            printToJson(response, result, callback);
        } catch (Exception e) {
            e.printStackTrace();
            Map<String, Object> result = new HashMap<String, Object>();
            result.put("res", "1");
            result.put("msg", e.getMessage());
            printToJson(response, result, callback);
            return;
        }
    }

    @RequestMapping(value = "updateChannel.do", method = {RequestMethod.GET, RequestMethod.POST})
    public void updateChannel(HttpServletRequest request, HttpServletResponse response, String channel, String callback) {
        try {
            queryChannel = channel;
            updateServerItmes();
            Map<String, Object> result = new HashMap<>();
            result.put("res", "0");
            result.put("serverItmes", serverItmes);
            result.put("platformItmes", platformItmes);
            printToJson(response, result, callback);
        } catch (Exception e) {
            e.printStackTrace();
            Map<String, Object> result = new HashMap<String, Object>();
            result.put("res", "1");
            result.put("msg", e.getMessage());
            printToJson(response, result, callback);
            return;
        }
    }

    @RequestMapping(value = "updatePlatform.do", method = {RequestMethod.GET, RequestMethod.POST})
    public void updatePlatform(HttpServletRequest request, HttpServletResponse response, String platform, String callback) {
        try {
            queryPlatform = platform;
            updateChannelItmes();
            Map<String, Object> result = new HashMap<>();
            result.put("res", "0");
            result.put("serverItmes", serverItmes);
            result.put("channelItmes", channelItmes);
            printToJson(response, result, callback);
        } catch (Exception e) {
            e.printStackTrace();
            Map<String, Object> result = new HashMap<>();
            result.put("res", "1");
            result.put("msg", e.getMessage());
            printToJson(response, result, callback);
            return;
        }
    }

	//平台的选择
	public void onlinePlatFormDataSelect() throws Exception {
		chartData2.setJsonStr(new ChartTool(dataList2, OnlinePlatFormData.class, chartData2.getSelectItem()).loadData(chartData2.getJsonType()));
	}
	
	//渠道的选择
	public void onlineChannelDataSelect() throws Exception {
		chartData3.setJsonStr(new ChartTool(dataList3, OnlineChannelData.class, chartData3.getSelectItem()).loadData(chartData3.getJsonType()));
	}
	
	//区服的选择
	public void onlineServerDataSelect() throws Exception {
		chartData4.setJsonStr(new ChartTool(dataList4, OnlineServerData.class, chartData4.getSelectItem()).loadData(chartData4.getJsonType()));
	}

    @Override
    public void search() {

    }

    @Override
	protected String csvContent() {
		return "在线分析";
	}

	@Override
    protected String csvFileName() {
	    return "在线分析";
    }

	public List<OnlieTendData> getDataList1() {
		return dataList1;
	}

	public void setDataList1(List<OnlieTendData> dataList1) {
		this.dataList1 = dataList1;
	}

	public ChartUIData getChartData1() {
		return chartData1;
	}

	public void setChartData1(ChartUIData chartData1) {
		this.chartData1 = chartData1;
	}

	public List<OnlinePlatFormData> getDataList2() {
		return dataList2;
	}

	public void setDataList2(List<OnlinePlatFormData> dataList2) {
		this.dataList2 = dataList2;
	}

	public ChartUIData getChartData2() {
		return chartData2;
	}

	public void setChartData2(ChartUIData chartData2) {
		this.chartData2 = chartData2;
	}

	public List<OnlineChannelData> getDataList3() {
		return dataList3;
	}

	public void setDataList3(List<OnlineChannelData> dataList3) {
		this.dataList3 = dataList3;
	}

	public ChartUIData getChartData3() {
		return chartData3;
	}

	public void setChartData3(ChartUIData chartData3) {
		this.chartData3 = chartData3;
	}

	public List<OnlineServerData> getDataList4() {
		return dataList4;
	}

	public void setDataList4(List<OnlineServerData> dataList4) {
		this.dataList4 = dataList4;
	}

	public ChartUIData getChartData4() {
		return chartData4;
	}

	public void setChartData4(ChartUIData chartData4) {
		this.chartData4 = chartData4;
	}
}
